package com.socialbank.web.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.nybank.fastdfs.FastDFSFile;
import com.nybank.fastdfs.FileManager;
import com.socialbank.web.FinanceBean.BankInsurance;
import com.socialbank.web.mapper.BankFundMapper;
import com.socialbank.web.mapper.BankInsuranceMapper;
import com.socialbank.web.service.BankInsuranceService;
import com.wqdata.auth.util.UserUtils;
@Service
public class BankInsuranceServiceImpl implements BankInsuranceService{
	@Autowired
	private SqlSession sqlSession;
	@Override
	public void save(MultipartFile file, BankInsurance bankInsurance) {
		BankInsuranceMapper mapper = sqlSession.getMapper(BankInsuranceMapper.class);
		String fileAbsolutePath="";//上传文件地址
		String groupName="";//文件服务器组名
		String remoteFileName="";//最终保存的文件名
		String fileType="";//文件类型
		Long deptId=UserUtils.getCurrrentDeptId();
		if(file!=null&&!file.isEmpty()){
			int beginIndex=file.getOriginalFilename().indexOf(".")+1;
			fileType=file.getOriginalFilename().substring(beginIndex);
				Date date=new Date();
				SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
				//以时间命名
				Map<String,Object> map=new HashMap<String,Object>();
				try{
				FastDFSFile dfsFile = new FastDFSFile(sdf.format(date).toString(), file.getBytes(), fileType);
				map = FileManager.upload(dfsFile);
				}catch(Exception e){
					e.printStackTrace();
				}
				System.out.println(fileAbsolutePath);
				fileAbsolutePath=(String)map.get("fileAbsolutePath");
				groupName=(String)map.get("groupName");
				remoteFileName=(String)map.get("remoteFileName");
				bankInsurance.setPicturePath(fileAbsolutePath);
				bankInsurance.setGroupName(groupName);
				bankInsurance.setRemoteFileName(remoteFileName);
		}
		bankInsurance.setDeptId(deptId);
		mapper.save(bankInsurance);
		
	}

	@Override
	public void deleteById(int id) {
		// TODO Auto-generated method stub
		BankInsuranceMapper mapper = sqlSession.getMapper(BankInsuranceMapper.class);
		BankInsurance bankInsurance=new BankInsurance();
		bankInsurance=mapper.findById(id);
		String picture_path=bankInsurance.getPicturePath();
		if(picture_path!=null&&!"".equals(picture_path)){
			String groupName=bankInsurance.getGroupName();
			String remoteFileName=bankInsurance.getRemoteFileName();
			try {
				FileManager.deleteFile(groupName, remoteFileName);//删除文件服务器上的文件
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		mapper.deleteById(id);
	}

	@Override
	public List<BankInsurance> getList(BankInsurance bankInsurance) {
		// TODO Auto-generated method stub
		BankInsuranceMapper mapper = sqlSession.getMapper(BankInsuranceMapper.class);
		Long deptId=UserUtils.getCurrrentDeptId();
		bankInsurance.setDeptId(deptId);
		List<BankInsurance> insurances=mapper.getList(bankInsurance);
		return insurances;
	}

	@Override
	public BankInsurance findById(int id) {
		BankInsuranceMapper mapper = sqlSession.getMapper(BankInsuranceMapper.class);
		BankInsurance bankInsurance=new BankInsurance();
		bankInsurance=mapper.findById(id);
		return bankInsurance;
	}

	@Override
	public void updateById(MultipartFile file, BankInsurance bankInsurance) {
		// TODO Auto-generated method stub
		BankInsuranceMapper mapper = sqlSession.getMapper(BankInsuranceMapper.class);
		String fileAbsolutePath="";//上传文件地址
		String groupName="";//文件服务器组名
		String remoteFileName="";//最终保存的文件名
		String fileType="";//文件类型
		if(file!=null&&!file.isEmpty()){
			int beginIndex=file.getOriginalFilename().indexOf(".")+1;
			fileType=file.getOriginalFilename().substring(beginIndex);
				Date date=new Date();
				SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
				//以时间命名
				Map<String,Object> map=new HashMap<String,Object>();
				try{
					FastDFSFile dfsFile = new FastDFSFile(sdf.format(date).toString(), file.getBytes(), fileType);
					map = FileManager.upload(dfsFile);
				}catch(Exception e){
					e.printStackTrace();
				}
				System.out.println(fileAbsolutePath);
				fileAbsolutePath=(String)map.get("fileAbsolutePath");
				groupName=(String)map.get("groupName");
				remoteFileName=(String)map.get("remoteFileName");
				bankInsurance.setPicturePath(fileAbsolutePath);
				bankInsurance.setGroupName(groupName);
				bankInsurance.setRemoteFileName(remoteFileName);
		}else{//如果没有上传图片，则不修改
			int id=bankInsurance.getId();
			BankInsurance insurance=new BankInsurance();
			insurance=mapper.findById(id);
			bankInsurance.setPicturePath(insurance.getPicturePath());
			bankInsurance.setGroupName(insurance.getGroupName());
			bankInsurance.setRemoteFileName(insurance.getRemoteFileName());
		}
		bankInsurance.setDeptId(UserUtils.getCurrrentDeptId());
		
		mapper.updateById(bankInsurance);
	}

	@Override
	public List<BankInsurance> getListAll(int deptId) {
		// TODO Auto-generated method stub
		BankInsuranceMapper mapper = sqlSession.getMapper(BankInsuranceMapper.class);
		List<BankInsurance> list=new ArrayList<BankInsurance>();
		list=mapper.getListAll(deptId);
		return list;
	}

}
